package it.terry.sms.test;


import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;

import android.content.res.AssetManager;
import android.telephony.SmsManager;
import android.test.ActivityInstrumentationTestCase2;
import android.util.Log;
import it.terry.smsmock.*;

public class SmsTesting extends ActivityInstrumentationTestCase2<SmsMock> {
	
	private static final String LOC_TAG = "SmS test";
	@SuppressWarnings("unused")
	private SmsMock myActivity;
	private MockProvider mymockprov;
	private SmsManager mysman;
	private List <String> data;
	
	
	
	public SmsTesting() {
	    super("it.terry.smsmock", SmsMock.class);
	  } // end of SpinnerActivityTest constructor definition
	
	@Override
	protected void setUp() throws Exception{
		super.setUp();
		
		setActivityInitialTouchMode(false);
		myActivity = getActivity();
		mysman = SmsManager.getDefault();
		mymockprov = new MockProvider(mysman,getInstrumentation().getContext());
		
	}
	
	public void testcase1(){
		myActivity = getActivity();
		invia_messaggi();
	}
	
	public void invia_messaggi(){
		AssetManager am = getInstrumentation().getContext().getAssets();
    	//prelievo
    	try {

    		data = new ArrayList<String>();
    		InputStream is = am.open("data.txt");
    		BufferedReader reader = new BufferedReader(new InputStreamReader(is));
    		String line = null;
    		while ((line = reader.readLine()) != null) {

    			data.add(line);
    		}
    		Log.d(LOC_TAG, data.size() + " lines");
    		Log.d(LOC_TAG, "Memorizzato"+data.toString());
          
    		

    	} catch (IOException e) {

    		e.printStackTrace();
    	}

    	
    	//USCITA
        for (String str: data) {

            try {

                Thread.sleep(3000);

            } catch (InterruptedException e) {

                e.printStackTrace();
            }
   
            // Invia Messaggio
            
            String[] parts = str.split(",");
            String messaggio = String.valueOf(parts[0]);
            String phoneNumber = String.valueOf(parts[1]);
            mymockprov.invia_messaggio(phoneNumber, messaggio);
	}
	}
}
